%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               Lab 1: Signal Representation in Matlab                    %
%                   EE558L: Section 1 (F: 1200-1440)                      %
%                            Dr. Nagaraj                                  %
%                     Author: Michael Spinali                             %
%                             813488956                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create Fresh Environment
close all
clear all
clc

%%%%%%%%%%%%%%% PART 1 %%%%%%%%%%%%%%%
figure('name','Part 1');
% Define Constants
F = 1000;               % Original Frequency
Fs = 16000;             % Sampling Frequency
Ts = 1/Fs;              % Sampling Period
Cycles = 4;             % Number of Cycles

%     No truely "Continous" signal can be generated in the digital 
% envirnoment. However, it can be simulated by using a small time increment
Tprec = 1/1000000;                  % Continous Time Precision
t = 0:Tprec:Cycles*(1/F)-Tprec;     % Time Vector (4 cycles)

% Continous Representation of Input Signal
xt = sin(2*pi*F*t);
subplot(2,1,1);
plot(t*1E3,xt);
grid on;
axis([0 Cycles*(1/F)*1E3 -1.1 1.1]);
title(sprintf('Continous Time X(t) = Sin(2*pi*%d*t)',F));
xlabel('Time (msec)');
ylabel('Amplitude');

% Number of Samples from Cont. Time being used (based F/Fs Ratio)
tHat = t(1:(length(t)/Cycles)*(F/Fs):end);
n = 0:length(tHat)-1;

% Discrete Representation of time vector
% Dependant on the sampling period, which determines the # of samples (n)
xn = sin(2*pi*n*F/Fs);

%Plot Stem Overlay (optional, but I think its a good visual)
hold on;
stem(tHat*1E3,xn,'MarkerSize', 5, 'Color', 'r');
hold off;

%Plot Sampled Signal
subplot(2,1,2);
plot(n,xn);
grid on;
axis([0 length(n) -1.1 1.1]);
title(sprintf('Discrete Time X[n] = Sin(2*pi*%f*n)',F/Fs));
xlabel('Time (samples)');
ylabel('Amplitude');

%%%%%%%%%%%%%%% PART 2 %%%%%%%%%%%%%%%
clear;
figure('name','Part 2');
% Define Constants
F = 15000;                  % Original Frequency
Fs = 16000;                 % Sampling Frequency
Ts = 1/Fs;                  % Sampling Period
Cycles = 8;                % Number of Cycles

%     No truely "Continous" signal can be generated in the digital 
% envirnoment. However, it can be simulated by using a small time increment
Tprec = 1/1000000;             % Continous Time Precision
t = 0:Tprec:Cycles*(1/F)-Tprec;    % Time Vector (4 cycles)

% Continous Representation of Input Signal
xt = sin(2*pi*F*t);
subplot(2,1,1);
plot(t*1E3,xt);
grid on;
axis([0 Cycles*(1/F)*1E3 -1.1 1.1]);
title(sprintf('Continous Time X(t) = Sin(2*pi*%d*t)',F));
xlabel('Time (msec)');
ylabel('Amplitude');

% Number of Samples from Cont. Time being used (based F/Fs Ratio)
tHat = t(1:(length(t)/Cycles)*(F/Fs):end);
n = 0:length(tHat)-1;

% Discrete Representation of time vector
% Dependant on the sampling period, which determines the # of samples (n)
xn = sin(2*pi*n*F/Fs);

%Plot Stem Overlay (optional, but I think its a good visual)
hold on;
stem(tHat*1E3,xn,'MarkerSize', 5, 'Color', 'r');
hold off;

%Plot Sampled Signal
subplot(2,1,2);
plot(n,xn);
grid on;
axis([0 length(n) -1.1 1.1]);
title(sprintf('Discrete Time X[n] = Sin(2*pi*%f*n)',F/Fs));
xlabel('Time (samples)');
ylabel('Amplitude');

%%%%%%%%%%%%%%% PART 3 %%%%%%%%%%%%%%%
clear;
figure('name','Part 3a');
% Define Constants
F = 1000;               % Original Frequency
Fs = 16000;             % Sampling Frequency
Ts = 1/Fs;              % Sampling Period
Cycles = 4;             % Number of Cycles

%     No truely "Continous" signal can be generated in the digital 
% envirnoment. However, it can be simulated by using a small time increment
Tprec = 1/1000000;             % Continous Time Precision
t = 0:Tprec:Cycles*(1/F)-Tprec;    % Time Vector (4 cycles)

%Show Sampling of Impulse Response
ht = exp(-1000*t);
subplot(2,1,1);
plot(t*1E3,ht,'LineWidth',2);
grid on;
axis([0 Cycles*(1/F)*1E3 -1.1 1.1]);
title('Continous Time h(t) = exp(-1000*t)');
xlabel('Time (msec)');
ylabel('Amplitude');

% Number of Samples from Cont. Time being used (based F/Fs Ratio)
tHat = t(1:(length(t)/Cycles)*(F/Fs):end);
n = 0:length(tHat)-1;

% Discrete Representation of time vector
% Dependant on the sampling period, which determines the # of samples (n)
xn = sin(2*pi*n*F/Fs);
hn = exp(-1000*Ts*n);

%Plot Stem Overlay (optional, but I think its a good visual)
hold on;
stem(tHat*1E3,hn,'MarkerSize', 5, 'Color', 'r');
hold off;

%Plot Sampled Signal
subplot(2,1,2);
plot(n,hn);
grid on;
axis([0 length(n) -1.1 1.1]);
title('Discrete Time h[n] = exp(-1000*Ts*n)');
xlabel('Time (samples)');
ylabel('Amplitude');

%Truncate hn, Convolve and plot
figure('name','Part 3b');
nN = 0:19;
hn=exp(-1000*nN*Ts);
Y = conv(xn,hn);
plot(Y);
grid on;
axis([0 length(nN)+length(n)-1 -3 4.5]);
title('h[n] * x[t]');
xlabel('Time (samples)');
ylabel('Amplitude');
